<?php
/**
 * Description of GroupController
 *
 * @author gun
 */
require_once 'BaseController.php';
class Admin_GroupController extends Admin_BaseController {
    
    public function init() {
        parent::init();
    }
    
    public function indexAction() {        
        $request = $this->getRequest();
        
        if ($this->httpRequest()->isXmlHttpRequest()) {
            $q = $this->getParam('q');
            $page = $this->getParam('page');
            
            $where = (isset($q) ? array('name LIKE ?' => "%$q%") : array());
            $stat = $this->_helper->ListPage('Admin_Model_GroupService', $page, $where);
            
            $options = array(
                'limit'=>
                    array(
                        $stat['perpage'] => $stat['offset'])                
            );
            
            $service = AppRegistry::loadService('Admin_Model_GroupService');
            $data = $service->getGroup($where, $options);
            
            if ($data) {
                $this->_helper->json(array(
                    'data' => $data, 
                    'pview' => $stat['pview'],
                    'offset' => $stat['offset'],
                    'fieldname' => 'description'
                ));
                
            } else {
                $this->_helper->json(array('error'=>'No Data Found.'));
            }
        }
    }
    
    public function saveAction() {
        $id = $this->getParam('id');
        $name = $this->getParam('name');
        $desc = $this->getParam('desc');
        
        $data = array(
            'name' => $name,
            'description' => $desc
        );
        
        foreach($this->baseUserLogin($id) as $key => $value) $data[ $key ] = $value;
        
        try {
            $service = AppRegistry::loadService('Admin_Model_GroupService');
            $saveGroup = (!isset($id) ? $service->doSave($data) : $service->doUpdate($data, $id));
            
            /**
             * Also save Group data to ACL
             * 
             */
            
            if (!isset($id)) {
                $menuService = AppRegistry::loadService('Admin_Model_MenuService');
                $menu = $menuService->getMenuForAcl();

                $baseUserInfo = $this->baseUserLogin($id);
                $aclService = AppRegistry::loadService('Admin_Model_AclService');

                $groupID = $service->getGroupByName( $name );
                
                $temp = array();
                foreach($menu as $key=>$value) {
                    $temp['menuID'] = $value['menuID'];
                    $temp['groupID'] = $groupID;
                    $temp['status'] = false;

                    foreach($baseUserInfo as $k => $v) $temp[ $k ] = $v;
                    $aclService->doSave($temp);
                }
            }
            
            $this->_helper->json(array('status'=>($saveGroup ? 'Save Success.' : 'Nothing changed.')));     
            
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getMessage()));            
        }
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function editAction() {
        $id = $this->getParam('id');
        $service = AppRegistry::loadService('Admin_Model_GroupService');
        $data = $service->getGroupById($id);
        
        $this->view->data = $data;
    }
    
    public function addAction() {        
        
    }

    public function deleteAction() {
        try {
            $id = $this->getParam('id');
            $service = AppRegistry::loadService('Admin_Model_GroupService');            

            $delete = $service->doDelete($id);
            $this->_helper->json(array('status'=>($delete ? 'Delete success.' : 'Nothing deleted.')));
            
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getTraceAsString()));
        }
    }        
    
}

?>
